package Client.GUI;

import Client.logic.ClientIF;
import Server.DataBase.SchoolClass;
import Server.DataBase.Student;
import Server.GUI.MyJTable;
import Server.Message.MessageAssignStudentToClass;
import Server.Message.MessageGetAllSchoolClass;
import Server.Message.MessageGetAllSchoolClassReplay;
import Server.Message.MessageGetStudentByClass;
import Server.Message.MessageGetStudentsReplay;
import Server.Message.MessageGetUnAssignStudents;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;

import java.util.ArrayList;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class AssignStudentToClassPanel extends MyJPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private ArrayList<SchoolClass> array =null;
	private ArrayList<Student> arrayUnassign =null;
	private ArrayList<Student> arrayAssign =null;
	private ArrayList<Student> arrayAdd =null;
	private ArrayList<Student> arrayRemove =null;
	private JComboBox comboBox;
	private MyJTable table;
	private MyJTable table2;
	private StudentTableModel stm;
	private StudentTableModel stm2;
	private String selectItem;
	private int idClass;
	private JButton btnAdd;
	private JButton btnRemove;
	private JButton btnApply;
	private JButton btnCancel;
	
	public AssignStudentToClassPanel( ClientIF client) {
		super(PanelType.ASSIGN_STUDENT_TO_CLASS_PANEL, client);
		setLayout(null);

		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"assign student to class.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(0, -15, 699, 99);
			add(lblAddClass);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				selectItem = (String) comboBox.getSelectedItem();
				int i=0;
				while(!array.get(i).getClassName().equals(selectItem))i++;
				idClass = array.get(i).getIdClass();
				getClient().sendMsgToServer(new MessageGetStudentByClass(idClass));
				MessageGetStudentsReplay rep = (MessageGetStudentsReplay) getClient().getMessageFromServer();
				arrayAssign = rep.getArray();
				stm2.setArray(arrayAssign);
			}
		});
		comboBox.setBounds(140, 99, 131, 20);
		add(comboBox);
		
		JLabel lblSelectClass = new JLabel("select class");
		lblSelectClass.setBounds(32, 102, 86, 14);
		add(lblSelectClass);
		
		
		init();
	}
	
	public void initArrays(){
		arrayAdd = new ArrayList<Student>();
		arrayRemove = new ArrayList<Student>();
		MessageGetAllSchoolClass msg = new MessageGetAllSchoolClass();
		
		getClient().sendMsgToServer(msg);
		MessageGetAllSchoolClassReplay msgRep = (MessageGetAllSchoolClassReplay) getClient().getMessageFromServer();
		array = msgRep.getArray();
		
		getClient().sendMsgToServer(new MessageGetUnAssignStudents());
		MessageGetStudentsReplay rep = (MessageGetStudentsReplay) getClient().getMessageFromServer();
		arrayUnassign = rep.getArray();
	}
	
	public void initTables(){
		stm = new StudentTableModel();		
		stm.setArray(arrayUnassign);
		table = new MyJTable(stm);
		table.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
 			public void valueChanged(ListSelectionEvent e) {
 				table.selectedRow = table.getTable().getSelectedRow();
 				btnAdd.setEnabled(true);
 			}
         });
		table.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		table.setBounds(10, 158, 249, 206);
		table.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		add(table);
		
		stm2 = new StudentTableModel();
		table2 = new MyJTable(stm2);
		table2.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
 			public void valueChanged(ListSelectionEvent e) {
 				table2.selectedRow = table2.getTable().getSelectedRow();
 				btnRemove.setEnabled(true);
 			}
         });
		table2.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		table2.setBounds(393, 158, 249, 206);
		table2.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		add(table2);
	}
	
	public void initBtn(){
		btnAdd = new JButton("add");
		btnAdd.setEnabled(false);
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int row = table.getSelectedItem();
				Student student = arrayUnassign.remove(row);
				arrayAssign.add(student);
				arrayAdd.add(student);
				stm.setArray(arrayUnassign);
				stm2.setArray(arrayAssign);
				comboBox.setEnabled(false);
				btnAdd.setEnabled(false);
				btnApply.setEnabled(true);
				btnCancel.setEnabled(true);
			}
		});
		btnAdd.setBounds(269, 210, 107, 23);
		add(btnAdd);
		
		btnRemove = new JButton("remove");
		btnRemove.setEnabled(false);
		btnRemove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Student student = arrayAssign.remove(table2.getSelectedItem());
				int i=0;
				boolean next = true;
				arrayUnassign.add(student);
				while(next){
					if (i<arrayAdd.size()){
						if (arrayAdd.get(i).getIdUser()==student.getIdUser()){
							arrayAdd.remove(i);
							next=false;
						}
					}
					else {
						arrayRemove.add(student);
						next=false;
					}
					i++;
				}
				stm.setArray(arrayUnassign);
				stm2.setArray(arrayAssign);
				btnRemove.setEnabled(false);
				comboBox.setEnabled(false);
				btnApply.setEnabled(true);
				btnCancel.setEnabled(true);
			}
		});
		btnRemove.setBounds(269, 266, 107, 23);
		add(btnRemove);
		
		btnApply = new JButton("Apply");
		btnApply.setEnabled(false);
		btnApply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getClient().sendMsgToServer(new MessageAssignStudentToClass(arrayAdd, arrayRemove, idClass));
				initArrays();
				comboBox.setEnabled(true);
				btnAdd.setEnabled(false);
				btnRemove.setEnabled(false);
				btnApply.setEnabled(false);
				btnCancel.setEnabled(false);
			}
		});
		btnApply.setBounds(196, 416, 89, 23);
		add(btnApply);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setEnabled(false);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				initArrays();
				stm.setArray(arrayUnassign);
				getClient().sendMsgToServer(new MessageGetStudentByClass(idClass));
				MessageGetStudentsReplay rep = (MessageGetStudentsReplay) getClient().getMessageFromServer();
				arrayAssign = rep.getArray();
				stm2.setArray(arrayAssign);
				comboBox.setEnabled(true);
				btnAdd.setEnabled(false);
				btnRemove.setEnabled(false);
				btnApply.setEnabled(false);
				btnCancel.setEnabled(false);
			}
		});
		btnCancel.setBounds(365, 416, 89, 23);
		add(btnCancel);
		
	
	}
	
	public void init(){
		initArrays();
		initTables();
		initBtn();
		for (int i=0; i<array.size(); i++){
			comboBox.addItem(array.get(i).getClassName());
		}
		
	}

	@Override
	public MyJPanel pushPanel() {
		return new AssignStudentToClassPanel(getClient());
	}
}
